﻿using System;
using System.Windows;
using System.Windows.Controls;
using Microsoft.Phone.Controls;

namespace SipCompensation
{
    public abstract class SipReadyPage : PhoneApplicationPage
    {
        private IDisposable _sipCompensation;

        protected SipReadyPage()
        {
            Loaded += PageLoaded;
        }

        public abstract ScrollViewer GetScrollViewer();

        void PageLoaded(object sender, RoutedEventArgs e)
        {
            if (_sipCompensation == null)
                _sipCompensation = SipHelper.EnableCompensationFor(GetScrollViewer());
        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);

            if (_sipCompensation != null)
            {
                _sipCompensation.Dispose();

                Dispatcher.BeginInvoke(() =>
                            {
                                _sipCompensation = SipHelper.EnableCompensationFor(GetScrollViewer());
                            });
            }
        }
        protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedFrom(e);

            if (_sipCompensation != null)
                _sipCompensation.Dispose();
        }
    }
}